<?php
/**
 * 固定传播基金专营店类别列表
 * @author qianxun
 *
 */
class spreadStoreModel extends MY_Model{
    function spreadStoreModel() {
        parent::MY_Model();
        $this->table = 'SpreadStore';
    }
    /**
     * 包装数据
     * {@inheritDoc}
     * @see MY_Model::packing()
     */
    function packing($list = array()){
        foreach ($list as $k => $v){
            if ($v['type'] == 1) {
                $v['storeTypeStr'] = '申报店';
            }elseif ($v['type'] == 2) {
                $v['storeTypeStr'] = '单独投放店';
            }else{
                $v['storeTypeStr'] = '协办店';
            }
            
            $list[$k] = $v;
        }
        return $list;
    }
    /**
     * 是否有预算申请权限
     * @param number $storeId
     * @param number $year
     * @return boolean
     */
    function isBudgetAdd($storeId, $year = 0){
        //如果不设置年份默认为当前配置年份
        $year || $year = $this->config->item('nowYear');
        $findData = $this->getData(array('storeId'=>$storeId,'timeUnit'=>0,'year'=>$year,'type' => array('in',array(1,2))));
        return $findData ? true : false;
    }
    /**
     * 获取预算申请专营店ID集
     * @param  integer $year 年份
     * @return array        专营店ID集
     */
    function budgetAddStoreIds($year = 0){
        $findList = $this->getList(array('timeUnit'=>0,'year'=>$year,'type' => array('in',array(1,2))));
        $storeIds = array();
        foreach ($findList as $k => $v) {
            $storeIds[] = (int)$v['storeId'];
        }
        return $storeIds;
    }
    /**
     * 获取主单店及协办店列表
     * @param number $storeId
     * @param number $year
     * @return array
     */
    function getXiebanStoreList($storeId, $year = 0){
        //如果不设置年份默认为当前配置年份
        $year || $year = $this->config->item('nowYear');
        $spreadStoreData = $this->getData(array('storeId'=>$storeId,'timeUnit'=>0,'year'=>$year));
        if (!$spreadStoreData){
            return array();
        }
        $storeList = array();
        $storeList[(int)$spreadStoreData['storeId']] = array('storeId'=>$spreadStoreData['storeId'],'storeName'=>$spreadStoreData['storeName'],'coding'=>$spreadStoreData['coding']);
        if ($spreadStoreData['type'] == 1){//申报店
            $this->CI->load->model('sellpointModel');
            //特殊城市，兼容其他城市专营店
            $specialCityList = $this->config->item('spreadSpecialCityList');
            if (isset($specialCityList[$spreadStoreData['city']])){
                $cityList = $specialCityList[$spreadStoreData['city']];
                $sameCityStoreList = $this->CI->sellpointModel->searchStore('', '', '', '', $cityList);
            }else{
                $sameCityStoreList = $this->CI->sellpointModel->searchStore('', '', '', '', $spreadStoreData['city']);
                $cityList = array($spreadStoreData['city']);
            }
            //删除掉单独投放店
            $singleSendStoreIds = array();
            $singleSendStoreList = $this->getList(array('city'=>$spreadStoreData['city'],'timeUnit'=>0,'year'=>$year,'type'=>2));
            foreach ($singleSendStoreList as $singleK => $singleV) {
                $singleSendStoreIds[] = (int)$singleV['storeId'];
            }

            //测试专营店列表
            $testStoreIds = array(3700=>'北区巡展虚拟账号');

            //筛选专营店
            foreach ($sameCityStoreList as $v){
                if (!in_array($v['city'], $cityList) || array_key_exists($v['userId'], $testStoreIds)) {
                    continue;
                }
                if ($v['userId'] != $spreadStoreData['storeId'] && !in_array($v['userId'], $singleSendStoreIds)){
                    $storeList[(int)$v['userId']] = array('storeId'=>$v['userId'],'storeName'=>$v['storeName'],'coding'=>$v['coding']);
                }
            }
        }elseif ($spreadStoreData['type'] == 2){//单独投放店
            
        }
        return $storeList;
    }
    /**
     * 查找制定专营店信息并以专营店ID为主键
     * @param array $ids
     */
    function getListByStoreIds($storeIds = array()){
        $findList = $this->getNewList(array('storeId'=>array('in', $storeIds)));
        $list = array();
        foreach($findList as $v){
            $list[(int)$v['storeId']] = $v;
        }
        return $list;
        
    }
}